package net.hn.hnms.biz.project.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.project.request.ProjectBudgetReq;
import net.hn.hnms.biz.project.service.ProjectBudgetService;
import net.hn.hnms.biz.project.vo.ProjectBudgetVo;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.web.controller.BaseController;
import net.hn.hnms.sys.common.log.annotation.Log;
import net.hn.hnms.sys.common.log.enums.BusinessType;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "项目预算", tags = {"项目预算"})
@RestController
@RequestMapping("/project/budget")
public class ProjectBudgetController extends BaseController {

    @Autowired
    private ProjectBudgetService projectBudgetService;

    /**
     * 新增项目预算
     */
    @ApiOperation("新增项目预算")
    @SaCheckPermission("project:budget:add")
    @PostMapping("/add")
    @Log(title = "新增项目预算", businessType = BusinessType.INSERT)
    public R<Void> add(@Validated @RequestBody ProjectBudgetReq projectBudgetReq) {
        return toAjax(projectBudgetService.insert(projectBudgetReq));
    }

    /**
     * 删除项目预算
     */
    @ApiOperation("删除项目预算")
    @SaCheckPermission("project:budget:delete")
    @ApiImplicitParam(name = "idList", value = "要删除的id值,用逗号间隔", required = true, allowMultiple = true, dataType = "String", paramType = "query")
    @PostMapping("delete")
    @Log(title = "删除项目预算", businessType = BusinessType.DELETE)
    public R<Void> delete(@RequestParam("idList") List<String> idList) {
        projectBudgetService.updateDeleteByIds(idList);
        return R.ok();
    }

    /**
     * 更新项目预算
     */
    @ApiOperation("更新项目预算")
    @SaCheckPermission("project:budget:update")
    @PostMapping("/update")
    @Log(title = "更新项目预算", businessType = BusinessType.UPDATE)
    public R<Void> update(@Validated @RequestBody ProjectBudgetReq projectBudgetReq) {
        projectBudgetService.updateById(projectBudgetReq);
        return R.ok();
    }

    /**
     * 项目预算详情
     */
    @ApiOperation("项目预算详情")
    @GetMapping("/{id}")
    public R<ProjectBudgetVo> detail(@PathVariable("id") String id) {
        return R.ok(projectBudgetService.selectDetailById(id));
    }


    /**
     * 项目预算列表
     */
    @ApiOperation("项目预算列表")
    @GetMapping("/page")
    @AutoSetMineCode
    public R<ResponsePage<ProjectBudgetVo>> page(ProjectBudgetReq projectBudgetReq, PageQuery pageQuery) {
            if(projectBudgetReq.getBeginDate() != null && projectBudgetReq.getEndDate() != null){
                projectBudgetReq.setStartTime(projectBudgetReq.getBeginDate());
                projectBudgetReq.setEndTime(projectBudgetReq.getEndDate());
            }
        return R.ok(projectBudgetService.selectPageList(projectBudgetReq, pageQuery));
    }



}
